package com.tanhua.server.controller;

import com.tanhua.domain.vo.PageResult;
import com.tanhua.server.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/smallVideos")
public class VideoController {

    @Autowired
    private VideoService videoService;

    /**
     * 小视频列表
     * GET/smallVideos
     */
    @GetMapping
    public ResponseEntity videoList(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize) {
        if (page < 1) {
            page = 1;
        }
        PageResult pageResult = videoService.findVideoList(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 视频上传
     * POST/smallVideos
     * @param videoThumbnail    视频封面文件
     * @param videoFile         视频文件
     * @return  null
     */
    @PostMapping
    public ResponseEntity uploadVideo(MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {
        return videoService.uploadVideo(videoThumbnail, videoFile);
    }

    /**
     * 视频用户关注
     * POST/smallVideos/:uid/userFocus
     */
    @PostMapping("/{uid}/userFocus")
    public ResponseEntity userFocus(@PathVariable("uid") Long targetId) {
        return videoService.saveUserFocus(targetId);
    }

    /**
     * 视频用户取消关注
     * POST/smallVideos/:uid/userUnFocus
     */
    @PostMapping("/{uid}/userUnFocus")
    public ResponseEntity userUnFocus(@PathVariable("uid") Long targetId) {
        return videoService.removeUserFocus(targetId);
    }

}
